package saucaca.Adapter;

import java.util.ArrayList;
import java.util.List;

import saucaca.Object.HomeData;
import saucaca.Object.RingtoneObject;
import saucaca.Service.MusicService;
import saucaca.Service.MusicService.MediaPlayerState;
import saucaca.Util.UtilMusic;
import saucaca.fragment.RingToneListSong;
import saucaca.fragment.RingtoneSetupFragment;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.lemon.kenhnhacvn.BaseEngine;
import com.lemon.kenhnhacvn.MainActivity;
import com.lemon.kenhnhacvn.R;

@SuppressLint({ "InflateParams", "NewApi" })
public class ExpandableRingtoneAdapter2 extends BaseExpandableListAdapter {
	private int[] groupStatus;
	ArrayList<RingtoneObject> listRingtoneObjects;
	ExpandableListView mExpandableListView;
	RingToneListSong ringToneListSong;
	Context mContext;
	RingtoneObject toneObject;

	String numberViettel = null;
	String numberVina = null;
	String numberMobi = null;
	String numberVietnamMobi = null;
	String telcoViettel;
	String telcoVina;
	String telcoMobi;
	String telcoVietnamMobi;
	String cuphapTaiViettel = "";
	String cuphapTangViettel = "";
	String cuphapTaiVina = "";
	String cuphapTangVina = "";
	String cuphapTaiMobi = "";
	String cuphapTangMobi = "";
	String cuphapTaiVietnamMobi = "";
	String cuphapTangVietnamMobi = "";

	String masoViettel = "1";
	String masoVina = "1";
	String masoMobi = "1";
	String masoVietnamMobi = "1";

	private Handler mHandler = new Handler();
	Runnable mUpdateTimeTask;

	public ExpandableRingtoneAdapter2(RingToneListSong ringToneListSong, ExpandableListView pExpandableListView, List<RingtoneObject> listRingtoneObjects) {
		this.ringToneListSong = ringToneListSong;
		mContext = ringToneListSong.getActivity();
		this.mExpandableListView = pExpandableListView;
		this.listRingtoneObjects = (ArrayList<RingtoneObject>) listRingtoneObjects;
		groupStatus = new int[this.listRingtoneObjects.size()];
		ringToneListSong.mediaPlayer = new MediaPlayer();
		setListEvent();
	}

	public void expandGroup(int groupPosition) {
		if (mExpandableListView.isGroupExpanded(groupPosition)) {
			mExpandableListView.collapseGroup(groupPosition);
		} else
			mExpandableListView.expandGroup(groupPosition);
	}

	private void setListEvent() {
		mExpandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {
			@Override
			public void onGroupExpand(int arg0) {
				groupStatus[arg0] = 1;
				int len = listRingtoneObjects.size();
				for (int i = 0; i < len; i++) {
					if (i != arg0) {
						mExpandableListView.collapseGroup(i);
					}
				}
			}
		});
		mExpandableListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
			@Override
			public void onGroupCollapse(int arg0) {
				groupStatus[arg0] = 0;
			}
		});
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public Object getGroup(int arg0) {
		return listRingtoneObjects.get(arg0);
	}

	@Override
	public int getGroupCount() {
		return listRingtoneObjects.size();
	}

	@Override
	public ChildHolder getChild(int groupPosition, int childPosition) {
		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return 0;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {
		final GroupHolder groupHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_ringtone_group, null);
			groupHolder = new GroupHolder();
			groupHolder.realativeGroup = (RelativeLayout) convertView.findViewById(R.id.relativeGroup);
			groupHolder.maso = (TextView) convertView.findViewById(R.id.item_ringtone_group_Maso);
			groupHolder.luottai = (TextView) convertView.findViewById(R.id.item_ringtone_group_luottai);
			groupHolder.title = (TextView) convertView.findViewById(R.id.ringtone_group_item_title);
			groupHolder.gia = (TextView) convertView.findViewById(R.id.item_ringtone_group_gia);
			convertView.setTag(groupHolder);
		} else {
			groupHolder = (GroupHolder) convertView.getTag();
		}
		groupHolder.realativeGroup.setOnClickListener(new View.OnClickListener() {
			@SuppressLint("NewApi")
			@Override
			public void onClick(View v) {
				expandGroup(groupPosition);
			}
		});
		groupHolder.title.setText(listRingtoneObjects.get(groupPosition).getTXNAME() + " - " + listRingtoneObjects.get(groupPosition).getNAME_SINGERS());
		groupHolder.maso.setText(listRingtoneObjects.get(groupPosition).getSONG_ID());
		groupHolder.luottai.setText(BaseEngine.convertStringLongtoInteger(listRingtoneObjects.get(groupPosition).getISDOWN()));
		// groupHolder.gia.setText(listRingtoneObjects.get(groupPosition).get);
		return convertView;
	}

	int mediaFileLengthInMilliseconds = 0;

	@SuppressLint("ClickableViewAccessibility")
	@Override
	public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		final ChildHolder holder = new ChildHolder();
		telcoViettel = mContext.getResources().getString(R.string.telco_viettel);
		telcoVina = mContext.getResources().getString(R.string.telco_vina);
		telcoMobi = mContext.getResources().getString(R.string.telco_mobi);
		telcoVietnamMobi = mContext.getResources().getString(R.string.telco_vietnammobi);
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_ringtone_child, null);
		}
		holder.buttonSetringtone = (LinearLayout) convertView.findViewById(R.id.item_ringtone_child_button_cainhaccho);
		holder.buttonGift = (LinearLayout) convertView.findViewById(R.id.item_ringtone_child_button_tang);
		holder.buttonPlay = (ImageView) convertView.findViewById(R.id.item_ringtone_child_playpause);
		holder.txtCurrent = (TextView) convertView.findViewById(R.id.item_ringtone_child_textbufferStart1);
		holder.txtLenght = (TextView) convertView.findViewById(R.id.item_ringtone_child_textbufferEnd1);
		holder.seekBarProgress = (SeekBar) convertView.findViewById(R.id.item_ringtone_child_seekBars1);
		holder.bar = (ProgressBar) convertView.findViewById(R.id.progressBar);
		
		holder.viettel_cuphap = (TextView) convertView.findViewById(R.id.ringtone_telco_viettel_maso);
		holder.vinaphone_cuphap = (TextView) convertView.findViewById(R.id.ringtone_telco_vinaphone_maso);
		holder.vietnammobi_cuphap = (TextView) convertView.findViewById(R.id.ringtone_telco_viennammobi_maso);
		holder.mobifone_cuphap = (TextView) convertView.findViewById(R.id.ringtone_telco_mobifone_maso);
		
		holder.viettel = (TextView) convertView.findViewById(R.id.ringtone_telco_viettel);
		holder.vinaphone = (TextView) convertView.findViewById(R.id.ringtone_telco_vinaphone);
		holder.vietnammobi = (TextView) convertView.findViewById(R.id.ringtone_telco_viennammobi);
		holder.mobifone = (TextView) convertView.findViewById(R.id.ringtone_telco_mobifone);
		
		holder.content = (TextView) convertView.findViewById(R.id.item_ringtone_child_sms);
		holder.viettel_tongdai = (TextView) convertView.findViewById(R.id.ringtone_telco_viettel_tongdai);
		holder.vinaphone_tongdai = (TextView) convertView.findViewById(R.id.ringtone_telco_vinaphone_tongdai);
		holder.vietnammobi_tongdai = (TextView) convertView.findViewById(R.id.ringtone_telco_viennammobi_tongdai);
		holder.mobifone_tongdai = (TextView) convertView.findViewById(R.id.ringtone_telco_mobifone_tongdai);

		holder.status_viettel = (TextView) convertView.findViewById(R.id.status_viettel);
		holder.status_vinaphone = (TextView) convertView.findViewById(R.id.status_vinaphone);
		holder.status_vietnammobi = (TextView) convertView.findViewById(R.id.status_vietnammobile);
		holder.status_mobifone = (TextView) convertView.findViewById(R.id.status_mobifone);

		holder.buttonViettel = (LinearLayout) convertView.findViewById(R.id.ringtone_viettel);
		holder.buttonVina = (LinearLayout) convertView.findViewById(R.id.ringtone_vinaphone);
		holder.buttonMobi = (LinearLayout) convertView.findViewById(R.id.ringtone_mobifone);
		holder.buttonVietNamm = (LinearLayout) convertView.findViewById(R.id.ringtone_vinammobile);

		holder.buttonPlay.setImageResource(R.drawable.nc_pause);
		try {
			if (ringToneListSong.mediaPlayer != null) {
				ringToneListSong.mediaPlayer.reset();
				ringToneListSong.mediaPlayer.release();
				ringToneListSong.mediaPlayer = null;
				ringToneListSong.mediaPlayer = new MediaPlayer();
			}
			ringToneListSong.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			ringToneListSong.mediaPlayer.setDataSource(listRingtoneObjects.get(groupPosition).getPATCH_DEMO());
			ringToneListSong.mediaPlayer.prepareAsync();
		} catch (Exception e) {
			e.printStackTrace();
		}

		holder.buttonGift.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				((MainActivity) mContext).switchContent(new RingtoneSetupFragment());
			}
		});

		ringToneListSong.mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
			@Override
			public void onPrepared(final MediaPlayer mp) {
				holder.bar.setVisibility(View.GONE);
				holder.buttonPlay.setVisibility(View.VISIBLE);
				mediaFileLengthInMilliseconds = ringToneListSong.mediaPlayer.getDuration();
				holder.txtLenght.setText(UtilMusic.milliSecondsToTimer(mediaFileLengthInMilliseconds));
				mUpdateTimeTask = new Runnable() {
					public void run() {
						try {
							long totalDuration = mp.getDuration();
							long currentDuration = mp.getCurrentPosition();
							holder.txtCurrent.setText("" + UtilMusic.milliSecondsToTimer(currentDuration));
							holder.seekBarProgress.setProgress(UtilMusic.getProgressPercentage(currentDuration, totalDuration));
							mHandler.postDelayed(this, 100);
						} catch (Exception e) {
							e.printStackTrace();
							try {
								ringToneListSong.mediaPlayer.start();
								mHandler.postDelayed(mUpdateTimeTask, 100);
								holder.seekBarProgress.setProgress(0);
								holder.seekBarProgress.setMax(100);
								if (ringToneListSong.mediaPlayer.isPlaying()) {
									if (MusicService.mState == MediaPlayerState.Playing) {
										Intent i = new Intent();
										i.setAction(MusicService.ACTION_PAUSE);
										((MainActivity) mContext).startService(i);
									}
									holder.buttonPlay.setImageResource(R.drawable.nc_play);
								}
							} catch (Exception e2) {
							}
						}
					}
				};
			}
		});

		ringToneListSong.mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer mp) {
				holder.buttonPlay.setImageResource(R.drawable.nc_pause);
			}
		});

		for (int i = 0; i < HomeData.homeListTelco.size(); i++) {
			if (HomeData.homeListTelco.get(i).getCODE().equals(telcoViettel)) {
				numberViettel = HomeData.homeListTelco.get(i).getSHORT_CODE();
				cuphapTaiViettel = HomeData.homeListTelco.get(i).getSYNTAX();
				cuphapTangViettel = HomeData.homeListTelco.get(i).getSEND_GIFTS();
			}
			if (HomeData.homeListTelco.get(i).getCODE().equals(telcoVina)) {
				numberVina = HomeData.homeListTelco.get(i).getSHORT_CODE();
				cuphapTaiVina = HomeData.homeListTelco.get(i).getSYNTAX();
				cuphapTangVina = HomeData.homeListTelco.get(i).getSEND_GIFTS();
			}
			if (HomeData.homeListTelco.get(i).getCODE().equals(telcoMobi)) {
				numberMobi = HomeData.homeListTelco.get(i).getSHORT_CODE();
				cuphapTaiMobi = HomeData.homeListTelco.get(i).getSYNTAX();
				cuphapTangMobi = HomeData.homeListTelco.get(i).getSEND_GIFTS();
			}
			if (HomeData.homeListTelco.get(i).getCODE().equals(telcoVietnamMobi)) {
				numberVietnamMobi = HomeData.homeListTelco.get(i).getSHORT_CODE();
				cuphapTaiVietnamMobi = HomeData.homeListTelco.get(i).getSYNTAX();
				cuphapTangVietnamMobi = HomeData.homeListTelco.get(i).getSEND_GIFTS();
			}
		}

		holder.viettel_cuphap.setText(cuphapTaiViettel);
		holder.vinaphone_cuphap.setText(cuphapTaiVina);
		holder.vietnammobi_cuphap.setText(cuphapTaiVietnamMobi);
		holder.mobifone_cuphap.setText(cuphapTaiMobi);
		
		holder.viettel_tongdai.setText(numberViettel);
		holder.vinaphone_tongdai.setText(numberVina);
		holder.mobifone_tongdai.setText(numberMobi);
		holder.vietnammobi_tongdai.setText(numberVietnamMobi);

		if (listRingtoneObjects.get(groupPosition).getVTL_SYNTAX() != null && listRingtoneObjects.get(groupPosition).getVTL_SYNTAX().length() > 0) {
			masoViettel = listRingtoneObjects.get(groupPosition).getVTL_SYNTAX();
			if (!masoViettel.equals("") && !masoViettel.equals("null")) {
				holder.buttonViettel.setVisibility(View.VISIBLE);
				holder.status_viettel.setVisibility(View.GONE);
				holder.viettel.setText(listRingtoneObjects.get(groupPosition).getVTL_SYNTAX());
				holder.buttonViettel.setClickable(true);
				holder.buttonViettel.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						BaseEngine.installRingtone(mContext, masoViettel, cuphapTaiViettel, cuphapTangViettel, numberViettel);
					}
				});
			}
		}

		if (listRingtoneObjects.get(groupPosition).getGPC_SYNTAX() != null && listRingtoneObjects.get(groupPosition).getGPC_SYNTAX().length() > 0) {
			masoVina = listRingtoneObjects.get(groupPosition).getGPC_SYNTAX();
			if (!masoVina.equals("") && !masoVina.equals("null")) {
				holder.vinaphone.setText(listRingtoneObjects.get(groupPosition).getGPC_SYNTAX());
				holder.buttonVina.setClickable(true);
				holder.buttonVina.setVisibility(View.VISIBLE);
				holder.status_vinaphone.setVisibility(View.GONE);
				holder.buttonVina.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {

						BaseEngine.installRingtone(mContext, masoVina, cuphapTaiVina, cuphapTangVina, numberVina);
					}
				});
			}
		}

		if (listRingtoneObjects.get(groupPosition).getVMS_SYNTAX() != null && listRingtoneObjects.get(groupPosition).getVMS_SYNTAX().length() > 0) {
			masoMobi = listRingtoneObjects.get(groupPosition).getVMS_SYNTAX();
			if (!masoMobi.equals("") && !masoMobi.equals("null")) {
				holder.buttonMobi.setClickable(true);
				holder.buttonMobi.setVisibility(View.VISIBLE);
				holder.status_mobifone.setVisibility(View.GONE);
				holder.vietnammobi.setText(listRingtoneObjects.get(groupPosition).getVNM_SYNTAX());
				holder.mobifone.setText(listRingtoneObjects.get(groupPosition).getVMS_SYNTAX());
				holder.buttonMobi.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						BaseEngine.installRingtone(mContext, masoMobi, cuphapTaiMobi, cuphapTangMobi, numberMobi);
					}
				});
			}
		}

		if (listRingtoneObjects.get(groupPosition).getVNM_SYNTAX() != null && listRingtoneObjects.get(groupPosition).getVNM_SYNTAX().length() > 0) {
			masoVietnamMobi = listRingtoneObjects.get(groupPosition).getVNM_SYNTAX();
			if (!masoVietnamMobi.equals("") && !masoVietnamMobi.equals("null")) {
				holder.buttonVietNamm.setClickable(true);
				holder.buttonVietNamm.setVisibility(View.VISIBLE);
				holder.status_vietnammobi.setVisibility(View.GONE);
				holder.buttonVietNamm.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						BaseEngine.installRingtone(mContext, masoVietnamMobi, cuphapTaiVietnamMobi, cuphapTangVietnamMobi, numberVietnamMobi);
					}
				});
			}
		}

		holder.buttonPlay.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!ringToneListSong.mediaPlayer.isPlaying()) {
					ringToneListSong.mediaPlayer.start();

					mHandler.postDelayed(mUpdateTimeTask, 100);
					holder.seekBarProgress.setProgress(0);
					holder.seekBarProgress.setMax(100);
					if (ringToneListSong.mediaPlayer.isPlaying()) {
						if (MusicService.mState == MediaPlayerState.Playing) {
							Intent i = new Intent();
							i.setAction(MusicService.ACTION_PAUSE);
							((MainActivity) mContext).startService(i);
						}
						holder.buttonPlay.setImageResource(R.drawable.nc_play);
					}

				} else {
					holder.buttonPlay.setImageResource(R.drawable.nc_pause);
					ringToneListSong.mediaPlayer.pause();
				}
			}
		});
		ringToneListSong.mediaPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
			@Override
			public void onBufferingUpdate(MediaPlayer mp, int percent) {
				holder.seekBarProgress.setSecondaryProgress(percent);
			}
		});

		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	class GroupHolder {
		boolean isExpanded = false;
		RelativeLayout realativeGroup;
		TextView title;
		TextView maso;
		TextView luottai;
		TextView gia;
	}

	class ChildHolder {
		ImageView buttonPlaypause;
		LinearLayout buttonSetringtone;
		LinearLayout buttonGift;
		LinearLayout buttonViettel;
		LinearLayout buttonVina;
		LinearLayout buttonMobi;
		LinearLayout buttonVietNamm;

		ProgressBar bar;
		ImageView buttonPlay;
		TextView txtCurrent;
		TextView txtLenght;
		SeekBar seekBarProgress;
		TextView status_viettel;
		TextView status_vinaphone;
		TextView status_mobifone;
		TextView status_vietnammobi;

		TextView content;
		TextView viettel;
		TextView vinaphone;
		TextView mobifone;
		TextView vietnammobi;
		TextView content_cuphap;
		TextView viettel_cuphap;
		TextView vinaphone_cuphap;
		TextView mobifone_cuphap;
		TextView vietnammobi_cuphap;
		TextView viettel_tongdai;
		TextView vinaphone_tongdai;
		TextView mobifone_tongdai;
		TextView vietnammobi_tongdai;
	}
}
